/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.android.inger;

//import android.R.color;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;



/**
 *
 * @author inger
 */
public class StatusReport {
	int defekt=0;
	Exception out=null;
	String statusReport[][]=new String[1000][5];
	int reportArrayCounter=0;
	int badLifts=0;
	int badStairs=0;
	public String[][] create(){
	reportArrayCounter=0;
	badLifts=0;
	badStairs=0;
		String baseurl="http://www.mvg-zoom.de/fahrgastinfo/do?_flowId=showStation&id=";
		String station[][]=new String[4][2];
		station[0]=new String[]{"OB","Ostbahnhof"};
		station[1]=new String[]{"OD","Odeonsplatz"};
		station[2]=new String[]{"MP","Marienplatz"};
		station[3]=new String[]{"UN","Universität"};
		//station[4]=new String[]{"KA","Stachus"};
		
		//(super.onCreate(savedInstanceState);
//		/** Create a new layout to display the view */
//		setContentView(R.layout.main);
//		LinearLayout layout = new LinearLayout(this);
//		layout.setOrientation(1);
//		layout.setVerticalFadingEdgeEnabled(true);
//		
//		layout.setBackgroundColor(getResources().getColor(R.color.white));
//		
		/** Create a new textview array to display the results */

		int lageStartIndex;
		int lageEndIndex;
		
		
		try {
			for(int s=0;s <station.length;s++){
						
		//String out=new String();
		String bh=new String();
		String bhString=new String();
		
		
		
		String id=new String();
		String lageDescription=new String();
		String type=new String();
		String typ=new String();
		String status=new String();
		String geplanterAusfall=new String();
		String description=new String();
		
		
		bhString= station[s][1];
				URL url = new URL(baseurl+station[s][0]);

				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse(new InputSource(url.openStream()));
				doc.getDocumentElement().normalize();


				NodeList nodeList=doc.getElementsByTagName("div");
				int defektEinzelnerBahnhof=0;


				for (int i = 0; i < nodeList.getLength(); i++) {
					status="";
					id="";
					lageDescription="";
					type="";
					typ="";
					status="";
					geplanterAusfall="";
					description="";
					Element itemList=(Element)nodeList.item(i);

					if(itemList.getAttribute("class").equalsIgnoreCase("infobox_inBetrieb")||itemList.getAttribute("class").equalsIgnoreCase("infobox_ausserBetrieb"))
						{
							type =itemList.getElementsByTagName("b").item(0).getTextContent();
								int typeStartIndex = type.indexOf(" ");  
							

							id=type.substring(typeStartIndex+1);
							typ=type.substring(0,typeStartIndex);
							description =itemList.getTextContent(); 
							lageStartIndex = description.indexOf("Lage");  
							lageEndIndex = description.indexOf("Status");

							lageDescription=description.substring(lageStartIndex, lageEndIndex);

							if(itemList.getAttribute("class").equalsIgnoreCase("infobox_ausserBetrieb"))

							{	status="bad";
								defektEinzelnerBahnhof++;
								

							} 

							else if(itemList.getAttribute("class").equalsIgnoreCase("infobox_inBetrieb")){
								//finde geplanten ausfall	
								status="ok";
							{ if(itemList.getAttribute("class").equalsIgnoreCase("infobox_ausserBetrieb_inlineInBetrieb")){
	//						
	//							geplanterAusfall =itemList.getTextContent();
	//							//geplanterAusfall =itemList.getElementsByTagName("div").item(0).getTextContent();
	//
	//								//	getTextContent();
	//						//intakte Elemente gruen ausgeben
	//							deviceStatus = new TextView(this);
	//							deviceStatus.setTextColor(getResources().getColor(R.color.black));
	//							deviceStatus.setText("xxx"+geplanterAusfall);
	//							ll.addView(deviceStatus);
							}
							}
							}else{status="";}
					
String statusReportLine[]=new String[5];

					statusReportLine[0]=bhString;
					statusReportLine[1]=typ;
					statusReportLine[2]=id;
					statusReportLine[3]=status;
					statusReportLine[4]=lageDescription;
					statusReport[reportArrayCounter]=statusReportLine;
					reportArrayCounter++;
						}
				}
				if (defektEinzelnerBahnhof==0){	
					
				}else{
				defekt++;
				}

			}	

				
		} catch (Exception e) {
			
			System.out.println("XML Pasing Excpetion = " + e);
			
			
			this.out=e;
		}	
			
		for(int p=0; p < reportArrayCounter; p++) {
			if(statusReport[p][3].toString().contains("bad") && statusReport[p][1].toString().contains("Lift"))
			{
				//&& statusReport[p][1].toString().contains("Lift")
				badLifts++;
			}
			if(statusReport[p][3].toString().contains("bad") && statusReport[p][1].toString().contains("Rolltreppe"))
			{
				//&& statusReport[p][1].toString().contains("Rolltreppe")
				badStairs++;
			}
		}
		return statusReport;
	
		//this.checkWhichIsDefekt();
	
}
/*
public void checkWhichIsDefekt(){
for(int i=0;i<this.reportArrayCounter;i++) {
	if(this.statusReport[i][3].equals("bad")&&this.statusReport[i][1].equalsIgnoreCase("lift"))badLifts++;
	if(this.statusReport[i][3].equalsIgnoreCase("bad")&&this.statusReport[i][1].equalsIgnoreCase("Rolltreppe"))badStairs++;
}
	
}	
*/


}